//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model
{
	import __AS3__.vec.Vector;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.dao.IDraftDAO;
	import reader.dao.IMediaDAO;
	import reader.model.vo.BlogDraftVO;
	import reader.model.vo.CountVO;
	import reader.model.vo.MediaVO;
	import reader.model.vo.UserVO;

	public class DraftProxy extends Proxy implements IDraftProxy
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "DraftProxy";
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var draftDAO:IDraftDAO;
		private var mediaDAO:IMediaDAO;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function DraftProxy()
		{
			super(NAME, []);
			draftDAO = readerModelLocator.sqliteDAOFactory.draftDAO;
			mediaDAO = readerModelLocator.sqliteDAOFactory.mediaDAO;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function deleteAccount():void
		{
			var user:UserVO = readerModelLocator.user;
			draftDAO.deleteDraftByUser(user);
			mediaDAO.deleteMediaByUser(user);
		}
		
		//----------------------------------------------------------------------------
		//
		// post draft
		//
		//----------------------------------------------------------------------------
		
		public function saveDraft(draft:BlogDraftVO):void
		{
			if(draftDAO.insertDraft(draft, readerModelLocator.user))
			{
				getUnPublishDraft(5);
			}
		}
		
		public function getPublishDraft(prefetch:int = -1):void
		{
			var draft:Vector.<BlogDraftVO> = draftDAO.selectPublishedDrafts(readerModelLocator.user.userId, prefetch);
			sendNotification(ApplicationFacade.DRAFT_PUBED_OK, draft);
		}
		
		public function getUnPublishDraft(prefetch:int = -1):void
		{
			var draft:Vector.<BlogDraftVO> = draftDAO.selectUnPublishedDrafts(readerModelLocator.user.userId, prefetch);
			sendNotification(ApplicationFacade.DRAFT_UNPUBED_OK, draft);
		}
		
		public function getAllDraft():void
		{
			var draft:Vector.<BlogDraftVO> = draftDAO.selectAllDrafts(readerModelLocator.user.userId);
			sendNotification(ApplicationFacade.DRAFT_ALL, draft);
		}
		
		public function updateDraft(draft:BlogDraftVO):void
		{
			draftDAO.updateDraft(draft);
		}
		
		public function updatePublishedDraft(draft:BlogDraftVO):void
		{
			draftDAO.updatePublishedDraft(draft);
		}
		
		public function publishDraft(draft:BlogDraftVO):void
		{
			draft.isPubed = true;
			draftDAO.publishDraft(draft, readerModelLocator.user);
			if(draft.draftId >= 0)
				draftDAO.deleteDraft(draft);
		}
		
		public function deleteDraft(draft:BlogDraftVO):void
		{
			draftDAO.deleteDraft(draft);
		}
		
		public function deleteDrafts(drafts:Vector.<BlogDraftVO>):void
		{
			draftDAO.deleteDrafts(drafts);
		}
		
		public function getCount():void
		{
			var count:CountVO = new CountVO();
			count.totalPost = draftDAO.getCount(readerModelLocator.user);
			count.totalDraft = draftDAO.getDraftCount(readerModelLocator.user);
			count.totalMedia = mediaDAO.getCount(readerModelLocator.user);
			sendNotification(ApplicationFacade.COUNT_DRAFT, count);
		}
		
		//----------------------------------------------------------------------------
		//
		// media proxy
		//
		//----------------------------------------------------------------------------
		
		public function saveMedia(media:MediaVO):void
		{
			mediaDAO.insertMedia(media, readerModelLocator.user.userId);
		}
		
		public function deleteMedia(media:MediaVO):void
		{
			mediaDAO.deleteMedia(media);
		}
		
		public function deleteMedias(medias:Vector.<MediaVO>):void
		{
			mediaDAO.deleteMedias(medias);
		}
		
		public function getAllMedia(prefetch:int = -1):void
		{
			var media:Vector.<MediaVO> = mediaDAO.selectAllMedia(readerModelLocator.user.userId, prefetch);
			sendNotification(ApplicationFacade.MEDIA_ALL, media);
		}
	}
}